<style type="text/css">
</style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <selectSearch :selectBaseInfo="selectBaseInfo" @post-data="getCompentensData"></selectSearch>
        <Form-item label="指定权限" prop="forgroupid">
            <!--<template v-for="(item,index) in data">-->
            <!--<div style="background-color: darkgray;margin-bottom: 10px;padding: 10px;border-radius: 4px">-->
            <!--<p style="font-size: 13px;font-weight: bold">{{item.GroupName}}</p>-->
            <!--<template v-for="(itemSub,indexSub) in item.SubGroups">-->
            <!--<p>{{itemSub.GroupName}}</p>-->
            <!--</template>-->
            <!--</div>-->
            <!--</template>-->
            <Tree @on-check-change="onCheckTreeEvent" :data="treeData" show-checkbox></Tree>

        </Form-item>
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <!--<i-button type="ghost" style="margin-left: 8px">取消</i-button>-->
        </Form-item>
    </i-form>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';
    import baseUtil from '../../assets/utils/base-util';
    import selectSearch from '../../components/selectSearch';

    export default {
        data() {
            return {
                formValidate: {
                    //添加字段
                },
                ruleValidate: {},
                fortypeArr: [],
                //选择人员a
                selectBaseInfo: {
                    "title": '选择人员',
                    "placeholder": '请输入姓名',
                    "postUrl": apiUtil.urls.userList,
                    'postType': 'user',
                },
                reqpeopleDept: '',
                //权限列表
                data: [],
                treeData: [],
                postTreeArr:[]
            }
        },

        mounted: function () {
            this.getGroupData();
        },
        components: {
            selectSearch
        },
        methods: {
            //添加
            handleSubmit(name) {

                let that = this;
                // let model = this.$refs[name].model;
                // this.$refs[name].validate((valid) => {
                    that.addData();
                // })
            },
            //获取选择搜索组件传来结果数据
            getCompentensData: function (value) {
                this.reqpeopleDept = value;
            },
            addData: function () {
                let that = this;
                let model ={};
                model.peopleId = this.reqpeopleDept.resultId;
                // model.rightTable = JSON.parse(that.postTreeArr);
                model.rightTable = JSON.stringify(that.postTreeArr);
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log('model:',model)
                apiUtil.post(that, apiUtil.urls.SetRights, params, function (res) {
                    that.$Loading.finish();

                    that.$Notice.open({
                        title: '通知',
                        desc: '添加成功 ',
                        duration: 0
                    });
                });
            },
            onCheckTreeEvent: function (data) {
                let that =this;
                console.log('tree的数据：', data);
                let postTreeArr =[];
                data.forEach(function (item, i) {
                    let treeItem = {};
                    if (item.isPost) {
                    } else {
                        treeItem.structureid = item.id;
                        // treeItem.title = item.title;
                        treeItem.allowuse = true;
                        postTreeArr.push(treeItem);
                    }

                });
                that.postTreeArr =postTreeArr;
                console.log('postTreeArr:', that.postTreeArr)
            },
            getStructuresData(group) {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();
                apiUtil.get(that, apiUtil.urls.ListStructures, para, function (res) {
                    that.$Loading.finish();
                    let newStrArr = [];
                    res.Msg.forEach(function (item, i) {
                        let itemObj = {};
                        // itemObj.GroupName = item.Interfaceurl + ' ' + '(' + item.Description + ')';
                        itemObj.GroupName = item.Description;
                        itemObj.Id = item.Id;
                        itemObj.Groupid = item.Groupid;
                        itemObj.SubGroups = [];
                        newStrArr.push(itemObj);
                    });
                    group.forEach(function (itemSub, j) {
                        newStrArr.forEach(function (item, i) {
                            if (item.Groupid == itemSub.Id) {
                                itemSub.SubGroups.push(item);
                            }
                        });
                    });
                    console.log('gruop:', group);
                    let treeData = [];
                    group.forEach(function (itemSub, j) {
                        let treeItem = {
                            children: [],
                        };
                        itemSub.SubGroups.forEach(function (item, i) {
                            let treeItemSub = {};
                            treeItemSub.children = item.SubGroups;
                            treeItemSub.title = item.GroupName;
                            treeItemSub.id = item.Id;
                            treeItem.isPost = true;
                            treeItem.children.push(treeItemSub);
                        });
                        treeItem.title = itemSub.GroupName;
                        treeItem.id = itemSub.Id;
                        treeItem.expand = true;
                        treeData.push(treeItem);
                    });
                    that.treeData = treeData;
                    console.log('treeData:', treeData);
                });

            },
            getGroupData: function () {
                let that = this;

                let para = {};

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.ListStructureGroup, para, function (res) {
                    that.data = res.Msg;
                    that.getStructuresData(res.Msg);
                });

            },


        }
    }
</script>
